//
//  DetailBottomTool.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/16.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "DetailBottomTool.h"

#import "LXShopCartController.h"

#import "LXOptionButton.h"

@interface DetailBottomTool()
@property(nonatomic,strong)NSMutableArray * roundViewArray;
@end
@implementation DetailBottomTool

-(id)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        UIButton * addToShopCart = [UIButton buttonWithType:UIButtonTypeCustom];
        [addToShopCart setTitle:@"加入购物车" forState:UIControlStateNormal];
        addToShopCart.backgroundColor = RGBColor(238,151,36);
        addToShopCart.height = self.height;
        addToShopCart.width = self.width * 0.35;
        addToShopCart.x = self.width - addToShopCart.width;
        [addToShopCart addTarget:self action:@selector(addToShopCartClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:addToShopCart];
        self.addToShopCart = addToShopCart;
        
        
        LXOptionButton * countDown = [[LXOptionButton alloc] init];
        [countDown setTitle:@"邀请好友砍价" forState:UIControlStateNormal];
        countDown.titleLabel.font = [UIFont systemFontOfSize:12];
        [countDown setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        countDown.width = [countDown.currentTitle sizeInAre:MaxSize font:countDown.titleLabel.font].width;
        countDown.height = self.height;
        [countDown setImage:[UIImage imageNamed:@"bargaining"] forState:UIControlStateNormal];
       
        [countDown addTarget:self action:@selector(countDownClick:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:countDown];
        self.countDown = countDown;
        
        
        LXOptionButton * focus = [[LXOptionButton alloc] init];
        [focus setTitle:@"关注" forState:UIControlStateNormal];
        focus.titleLabel.font = [UIFont systemFontOfSize:12];
        focus.width = [focus.currentTitle sizeInAre:MaxSize font:focus.titleLabel.font].width;
        focus.height = self.height;
        [focus setImage:[UIImage imageNamed:@"Focus-0"] forState:UIControlStateNormal];
       
        [focus addTarget:self action:@selector(focusClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:focus];
        self.focus = focus;
        
        LXOptionButton * shopCart = [[LXOptionButton alloc] init];
        [shopCart setTitle:@"购物车" forState:UIControlStateNormal];
        shopCart.titleLabel.font = [UIFont systemFontOfSize:12];
        shopCart.width = [shopCart.currentTitle sizeInAre:MaxSize font:shopCart.titleLabel.font].width;
        shopCart.height = self.height;
        [shopCart setImage:[UIImage imageNamed:@"shopping-0"] forState:UIControlStateNormal];
       
        [shopCart addTarget:self action:@selector(shopCartClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:shopCart];
        self.shopCart = shopCart;
        
        UILabel * countLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        countLabel.backgroundColor = [UIColor redColor];
        [self addSubview:countLabel];
        countLabel.textAlignment = NSTextAlignmentCenter;
        countLabel.font = [UIFont systemFontOfSize:10];
        countLabel.textColor = [UIColor whiteColor];
        self.countLabel = countLabel;
        
    }
    return self;
}
-(void)setCount:(NSInteger)count{
    _count = count;
    self.countLabel.text = [NSString stringWithFormat:@"%ld",_count];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat space = (self.width - self.addToShopCart.width- self.focus.width - self.shopCart.width)/3;
    self.countDown.x = space;
    self.focus.x = space;
    self.shopCart.x = self.focus.right + space;
    
    self.countLabel.x = self.shopCart.right-5;
    [self.countLabel addRadius:10];
    self.countLabel.y = 5;
    self.countLabel.size = CGSizeMake(20, 20);
}


-(void)addToShopCartClick:(UIButton*)button{
    if(self.delegate && [self.delegate respondsToSelector:@selector(DetailBottomTool:didClickAddToShopCartButton:)]){
        [self.delegate DetailBottomTool:self didClickAddToShopCartButton:button];
    }
}

//动画结束调用该方法
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    self.count+=1;
    self.countLabel.text=[NSString stringWithFormat:@"%ld",self.count];
    [self.roundViewArray.firstObject removeFromSuperview];
    [self.roundViewArray removeObjectAtIndex:0];
}

-(void)addAnimation{
    UIView * roundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    roundView.backgroundColor = RandColor;
    [roundView addRadius:10];
    [self.roundViewArray addObject:roundView];
    roundView.x = (self.width - roundView.width)+roundView.width*0.5;;
    roundView.y = (self.height - roundView.height)-roundView.height*0.5;
    [self addSubview:roundView];
    //起始点
    CGPoint startPoint= CGPointMake(roundView.x, roundView.y);
    //结束点
    CGPoint endPoint= CGPointMake(self.countLabel.x+self.countLabel.width*0.5, self.countLabel.y+self.countLabel.height*0.5);
    
    //控制点
    CGPoint controlPoint = CGPointMake(endPoint.x+((startPoint.x-endPoint.x))*0.5, -100);
    
    
    //绘制贝塞尔曲线
    UIBezierPath * movePath = [UIBezierPath bezierPath];
    [movePath moveToPoint:startPoint];
    [movePath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    
    //关键帧动画
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [keyFrameAnimation setDuration:0.4];
    keyFrameAnimation.path = movePath.CGPath;
    keyFrameAnimation.fillMode = kCAFillModeForwards;
    keyFrameAnimation.removedOnCompletion = NO;
    [roundView.layer addAnimation:keyFrameAnimation forKey:@"movingAnimation"];

}

-(void)countDownClick:(UIButton*)button{
    
}

-(void)focusClick:(UIButton*)button{
    __block MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
    [[LXDataManager manager] addFocusProduct:self.product complete:^(BOOL success, id res) {
        if(success){
            if([res[@"data"][@"error"] isEqualToString:@"用户已关注过该商品"]){
                hud.labelText = @"您已经关注改商品啦!";
                [hud hide:YES afterDelay:1];
                
            }else{
                hud.labelText = @"关注成功";
                [hud hide:YES afterDelay:1];
            }
        }else{
            hud.labelText = @"关注失败";
            [hud hide:YES afterDelay:1];
        }
        
    }];
}

-(void)shopCartClick:(UIButton*)button{
    
    if(self.delegate&&[self.delegate respondsToSelector:@selector(DetailBottomTool:didClickShopCartButton:)]){
        [self.delegate DetailBottomTool:self didClickShopCartButton:button];
    }

}

-(NSMutableArray*)roundViewArray{
    if(_roundViewArray == nil){
        _roundViewArray = [NSMutableArray array];
    }
    return _roundViewArray;
}

@end
